package lormi.meipin.common;

import io.rong.ApiHttpClient;
import io.rong.models.FormatType;
import io.rong.models.SdkHttpResult;
import io.rong.models.TxtMessage;
import lormi.meipin.exception.LMException;

import java.util.ArrayList;
import java.util.List;

/**
 * 在线聊天融云
 * 
 * */
public class LMRongYunAPI {
	
	//测试环境
	private static final String key = "x18ywvqf8hhyc";
	private static final String secret = "IqFLymEhUt86";

	//正式环境
//	private static final String key = "x4vkb1qpvrrkk";
//	private static final String secret = "cmhtKcX5OO8ROj";

	private static SdkHttpResult result = null;
	
	public static SdkHttpResult getRongYunToken(String userid,String username,String photo) throws LMException{
		
		try {
			result = ApiHttpClient.getToken(key, secret, userid,username,
					photo, FormatType.json);
		} catch (Exception e) {
			throw new LMException(180001);
		}
		return result; 
	}
	/**
	 * id 1~20,系统留用
	 * */
	private static final String focused_id = "99999999";//被关注通知ID,8

	//消息推送
	private static SdkHttpResult systemPush(String fromID,String name,String photo,String toID,String message) throws LMException{
		List<String> toIDs = new ArrayList<>();
		toIDs.add(toID);
		try {
			ApiHttpClient.getToken(key, secret,fromID,name,photo, FormatType.json);
			result = ApiHttpClient.publishSystemMessage(key, secret,fromID, toIDs,
					new TxtMessage(message),message,"pushd", FormatType.json);
		} catch (Exception e) {
			throw new LMException();
		}
		System.out.println("====融云推送===fromID:"+fromID+",toID:"+toID+",message:"+message);
		return result;
	}
	
	/**
	 * 通知推送,包括被关注推送、被查看推送、精准匹配推送
	 * 1=》关注（收藏）通知
	 * 2=》查看通知
	 * 3=》精准匹配通知
	 * */
	public static SdkHttpResult push(String toID,String name,int type) throws LMException{
		String message = Constants.getContent(type);

		return systemPush(focused_id,"通知","http://m.meipins.com/img/icons/no-chat.png", toID, name+" "+message);
	}
	//被查看通知推送
	//精准匹配通知推送
	
//	public static void main(String[] args) throws Exception {
//		List<String> toIDs = Arrays.asList("984");
//		
//		System.out.println(sendSystem(toIDs,"836关注了你").getResult());
//		
//	}
	
	
	public static void main(String[] args) throws Exception {
		
//		System.out.println(JSONObject.toJSON(LMRongYunAPI.push("978","cjf",12)));
		
	}
	
}
